$.post("CartGoods",{ method : "getAllGoods" },
			function(data){
		
		if(data==null)
			return;
		
		var goods = JSON.parse(data);
		
		
		var totalspent = 0;
		
		for(i=0;i<goods.length;i++){
			var text = "<tr>\r\n" + 
			"				<td class=\"pro-thumbnail\"><a href=\"#\"><img class=\"img-fluid\" src=\""+ goods[i].goodsrc  +"\" alt=\"Product\" /></a></td>\r\n" + 
			"               <td class=\"pro-title\"><a href=\"#\">"+ goods[i].goodsname  +"</a></td>\r\n" + 
			"               <td class=\"pro-price\"><span>$"+ goods[i].price +"</span></td>\r\n" + 
			"               <td class=\"pro-quantity\">\r\n" + 
			"               <div class=\"pro-qty\"><input class=\"changeQuantity\"type=\"text\" value=\""+ goods[i].goodsnum +"\"></div>\r\n" + 
			"               </td>\r\n" + 
			"               <td class=\"pro-subtotal\"><span>"+"$"+(goods[i].goodsnum * goods[i].price).toFixed(2)+"</span></td>\r\n" + 
			"               <td class=\"pro-remove\"><div style=\"cursor:pointer\" class=\" removeGoods \"><i class=\"fa fa-trash-o\"></i></div></td>\r\n" + 
			" 				<td style=\"display: none;\" class=\"goodsid \">"+ goods[i].goodsid +"</td>"+
			"           </tr> "
				
			
			totalspent+=goods[i].goodsnum * goods[i].price;

			$("#CartContent").append(text);
		}
			
		$("#totalspent").text("$"+(totalspent.toFixed(2)));
		$("#total-amount").text("$"+(totalspent.toFixed(2)));
	});

$(function(){
	
	$(".dec").click(function(){
		
		var temp =$(this).parent().parent().siblings("td.pro-price").text();
		var price = parseFloat(temp.substring(1));
		var quantity = $(this).siblings("input").val();
		
		
		$.post("CartGoods",{ method:"changeQuantity" , goodsid:$(this).parent().parent().siblings("td.goodsid").text(), goodsnum:quantity});
		
		
		var c="$"+(price*quantity).toFixed(2);
		$(this).parent().parent().siblings("td.pro-subtotal").html("<span>"+c+"</span>");
		
		
		var spent = parseFloat(($("#totalspent").text()).substring(1));
		spent-= price;
		
		$("#totalspent").text("$"+(spent.toFixed(2)));
		$("#total-amount").text("$"+(spent.toFixed(2)));
		
	})
	
	$(".inc").click(function(){
		
		var temp =$(this).parent().parent().siblings("td.pro-price").text();
		var price = parseFloat(temp.substring(1));
		var quantity = $(this).siblings("input").val();
		
		
		$.post("CartGoods",{ method:"changeQuantity" , goodsid:$(this).parent().parent().siblings("td.goodsid").text(), goodsnum:quantity});
		
		
		var c="$"+(price*quantity).toFixed(2);
		$(this).parent().parent().siblings("td.pro-subtotal").html("<span>"+c+"</span>");

		var spent = parseFloat(($("#totalspent").text()).substring(1));
		spent += price;
		
		$("#totalspent").text("$"+(spent.toFixed(2)));
		$("#total-amount").text("$"+(spent.toFixed(2)));
		
	})
	
	
	
	$(".removeGoods").click(function (){
		
		
		$.post("CartGoods",{ method:"changeQuantity" , goodsid:$(this).parent().siblings("td.goodsid").text(), goodsnum:"-1"});
		
		$(this).parent().parent().remove();
		
	})
	
	$("#applyCoupon").click(function(){
		$.post("Servet",{param:"code"},function(data){
			
			//data为返回的验证码
			//0不可用
			//1可用
			//json解析
			var obj = JSON.parse(data);
			if(obj.data=="1"){
				$("#shipping").text("$"+obj.amount);
				
				var price = parseFloat($("#total-amount").text().substring(1));
				price -=obj.amount;
				$("#total-amount").text("$"+(price.toFixed(2)));
			}
			
		});
	})
	
	
})